//
//  STLScrollView.m
//  STL_ Animation
//
//  Created by 孙健 on 16/3/30.
//  Copyright © 2016年 STL. All rights reserved.
//

#import "STLScrollView.h"

@interface STLScrollView ()<UIScrollViewDelegate>

@property (nonatomic, assign) NSInteger numberOfIndex;
@property (nonatomic, assign) CGPoint touchedPoint;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *textArr;

@end

@implementation STLScrollView

- (instancetype)initWithFrame:(CGRect)frame withTextArr:(NSArray <NSString *> *)textArr withIndex:(NSInteger)index{
    self = [super initWithFrame:frame];
    if (self) {
        self.numberOfIndex = index;
        self.textArr = [NSMutableArray array];
        for (int i = 0; i < 2; i ++) {
            for (NSString *str in textArr) {
                [self.textArr addObject:str];
            }
        }
        
        NSLog(@"%@", self.textArr);
        [self creatArrowImageView];
        [self creatScrollView];
    }
    return self;
}

- (void)creatArrowImageView {
    UIImageView *arrowImageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.bounds.size.width - 10) / 2, 0, 10, 10)];
    arrowImageView.image = [UIImage imageNamed:@"arrow"];
    [self addSubview:arrowImageView];
}

- (void)creatScrollView {
    self.scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(self.frame.size.width / 8 * 3, 10, self.bounds.size.width / 4, self.bounds.size.height - 10)];
    
    self.scrollView.contentSize= CGSizeMake(self.scrollView.frame.size.width * self.textArr.count, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.backgroundColor= [UIColor clearColor];
    self.scrollView.clipsToBounds = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
//    [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width * self.numberOfIndex, 0) animated:NO];
    if (self.scrollView.contentOffset.x < self.scrollView.frame.size.width * (self.numberOfIndex - 0.5)) {
        [self.scrollView setContentOffset:CGPointMake(self.scrollView.bounds.size.width * self.numberOfIndex + self.textArr.count / 2 * self.scrollView.frame.size.width, 0) animated:NO];
    } else if (self.scrollView.contentOffset.x > self.scrollView.frame.size.width * (self.numberOfIndex - 0.5 + self.textArr.count / 2)) {
        [self.scrollView setContentOffset:CGPointMake(self.scrollView.bounds.size.width * self.numberOfIndex - self.textArr.count / 2 * self.scrollView.frame.size.width, 0) animated:NO];
    } else {
         [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width * self.numberOfIndex, 0) animated:NO];
    }
    
    for (int i = 0; i < self.textArr.count; i++) {
        
        UILabel *typeLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
        typeLabel.tag = i;
        typeLabel.textAlignment = NSTextAlignmentCenter;
        typeLabel.textColor = [UIColor colorWithRed:8/255.0 green:146/255.0 blue:114/255.0 alpha:1];
        typeLabel.font = [UIFont systemFontOfSize:14];
        typeLabel.text = [self.textArr objectAtIndex:i];
        
        [self.scrollView addSubview:typeLabel];
    }
    [self addSubview:self.scrollView];
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint offSetPoint = self.scrollView.contentOffset;// 偏移量
    CGFloat width = self.scrollView.frame.size.width; // scroll大小
    
    
    //    if (offSetPoint.x < width *2.5 &&offSetPoint.x > width * 1.5)
    //    {
    //        [scrollView setContentOffset:CGPointMake(offSetPoint.x + self.textArr.count / 2 * width, 0) animated:NO];
    //
    //    }
    if (offSetPoint.x < width * (self.numberOfIndex - 0.5)) {
        [scrollView setContentOffset:CGPointMake(offSetPoint.x + self.textArr.count / 2 * width, 0) animated:NO];
    }
    
    
    //    if (offSetPoint.x < width * 9.5 && offSetPoint.x > width * 8.5)
    //    {
    //        [scrollView setContentOffset:CGPointMake(offSetPoint.x - self.textArr.count / 2 * width, 0) animated:NO];
    //
    //    }
    
    if (offSetPoint.x > width * (self.numberOfIndex - 0.5 + self.textArr.count / 2)) {
        [scrollView setContentOffset:CGPointMake(offSetPoint.x - self.textArr.count / 2 * width, 0) animated:NO];
    }
    
}


- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    
    UIScrollView *scrollView = [[self subviews] lastObject];
    
    if ([self pointInside:point withEvent:event]) {
        
        return scrollView;
    }
    return nil;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    NSInteger indexItem = (NSInteger)(self.scrollView.contentOffset.x / self.scrollView.bounds.size.width);
    
    
    //    UILabel *label = [self.scrollView viewWithTag:indexItem];
    //    label.textColor = [UIColor greenColor];
    if (indexItem >= self.textArr.count / 2) {
        indexItem = indexItem - self.textArr.count / 2;
    }
    [self.delegate indexOfDetailId:indexItem];
    NSLog(@"%ld", indexItem);
}

@end
